using RockHopper.Common.Data;
using RockHopper.Common.Services;
using RockHopper.TrainingCalculator.WinRT.Common;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace RockHopper.TrainingCalculator.WinRT.Controls
{
    public class HeartRateControl : Control
    {
        private static readonly ISettingsRepository settingsRepository;
        private static readonly ICalculationService calculationService;
        private static readonly Style textBlockStyle =
            Common.Resources.TrainingStyles.Get<Style>("ValueTextBoxStyle");
        private static readonly Style textBlockErrorStyle =
            Common.Resources.TrainingStyles.Get<Style>("ValueTextBoxErrorStyle");
        private TextBox restingHr;
        private TextBox maximumHr;
        private TextBlock easyText;
        private TextBlock marathonText;
        private TextBlock thresholdText;
        private TextBlock intervalText;

        public HeartRateControl()
        {
            DefaultStyleKey = typeof(HeartRateControl);
        }

        static HeartRateControl()
        {
            if (!DesignMode.DesignModeEnabled)
            {
                settingsRepository = Global.Container.Resolve<ISettingsRepository>();
                calculationService = Global.Container.Resolve<ICalculationService>();
            }
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            restingHr = (TextBox) GetTemplateChild("RestingHeartRatePart");
            restingHr.Text = settingsRepository.RestingHeartRate.ToString();
            restingHr.TextChanged += (s, e) => CalculateHeartRateZones();
            maximumHr = (TextBox)GetTemplateChild("MaximumHeartRatePart");
            maximumHr.Text = settingsRepository.MaximumHeartRate.ToString();
            maximumHr.TextChanged += (s, e) => CalculateHeartRateZones();
            easyText = (TextBlock) GetTemplateChild("EasyPart");
            marathonText = (TextBlock)GetTemplateChild("MarathonPart");
            thresholdText = (TextBlock)GetTemplateChild("ThresholdPart");
            intervalText = (TextBlock)GetTemplateChild("IntervalPart");

            CalculateHeartRateZones();
        }

        private void CalculateHeartRateZones()
        {
            int restingValue;
            int maximumValue;

            if (int.TryParse(restingHr.Text, out restingValue)
                && int.TryParse(maximumHr.Text, out maximumValue)
                && restingValue > 0
                && maximumValue > 0
                && maximumValue > restingValue)
            {
                restingHr.Style = textBlockStyle;
                maximumHr.Style = textBlockStyle;

                settingsRepository.RestingHeartRate = restingValue;
                settingsRepository.MaximumHeartRate = maximumValue;
                var zones = calculationService.CalculateHeartRateZones(restingValue, maximumValue);
                easyText.Text = string.Format("{0}-{1}bpm", zones.EasyLower, zones.EasyUpper);
                marathonText.Text = string.Format("{0}-{1}bpm", zones.MarathonLower, zones.MarathonUpper);
                thresholdText.Text = string.Format("{0}-{1}bpm", zones.ThresholdLower, zones.ThresholdUpper);
                intervalText.Text = string.Format("{0}-{1}bpm", zones.IntervalLower, zones.IntervalUpper);
            }
            else
            {
                restingHr.Style = textBlockErrorStyle;
                maximumHr.Style = textBlockErrorStyle;
            }
        }
    }
}